<script lang="ts" setup>
import { ref, reactive, watch } from "vue"
import { RefreshRight, Search, Refresh } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { province, provinceEdit } from "@/api/combo"
import { ElMessage } from "element-plus"
import { toRefs } from "vue"

defineOptions({
  name: "grapOrderSet"
})
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const loading = ref<boolean>(false)

// #region 搜索
const searchFormRef = ref<any>(null)
const searchData = reactive({
  dept: "",
  superId: "",
  type: ""
})
// 查询按键
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

// 重置按键
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

//#region 查
const tableData = ref<any>([])
const getTableData = () => {
  loading.value = true
  province({ ...searchData, page: paginationData.currentPage, size: paginationData.pageSize })
    .then((res: any) => {
      paginationData.total = res.data.total
      tableData.value = res.data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

// 刷新数据
const handleRefresh = () => {
  getTableData()
}
const dialogVisible = ref<boolean>(false)
const formData = reactive<any>({ data: {} })
const { data } = toRefs(formData)

const handleUpdate = (row: any) => {
  dialogVisible.value = true
  data.value = JSON.parse(JSON.stringify(row))
  // provinceEdit({
  //   keyName: row.keyName,
  //   number: row.number
  // }).then((res: any) => {
  //   if (res.code == 200) {
  //     ElMessage.success("修改成功")
  //   }
  // })
}
const resetForm = () => {
  data.value = {}
}
const handleCreate = () => {
  provinceEdit({
    keyName: data.value.keyName,
    number: data.value.number
  }).then((res: any) => {
    if (res.code == 200) {
      ElMessage.success("修改成功")
      dialogVisible.value = false
      getTableData()
    }
  })
}
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
//#endregion

// #region 清除验证  清除规则报错信息
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="dept" label="部门">
          <el-input v-model.trim="searchData.dept" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="superId" label="上级编码">
          <el-input v-model.trim="searchData.superId" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="type" label="类型">
          <el-input v-model.trim="searchData.type" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="刷新表格">
            <el-button type="primary" :icon="RefreshRight" circle @click="handleRefresh" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="keyName" label="省内外Key" align="center" />
          <el-table-column prop="dept" label="部门" align="center" />
          <el-table-column prop="superId" label="上级编码" align="center" />
          <el-table-column prop="number" label="录单量" align="center" />
          <el-table-column prop="type" label="类型" align="center" />
          <el-table-column fixed="right" label="操作" width="80" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 修改 -->
    <el-dialog v-model="dialogVisible" :title="'修改'" @close="resetForm" width="30%">
      <el-form ref="formRef" :model="data" label-width="100px" label-position="left">
        <el-form-item prop="number" label="录单量">
          <el-input v-model="data.number" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
.Signature-matter {
  margin-bottom: 20px;
  border: 1px solid #6cf;
  background: #e5f0ff;
  padding: 10px 14px;
  border-radius: 5px;
  font-size: 12px;
}
:deep(.el-radio-group) {
  display: block;
}
:deep(.el-radio.el-radio--large) {
  display: block !important;
}
</style>
